package com.xxl.job.executor.service.impl;


import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.common.util.DBUtils;
import com.xxl.job.executor.common.util.SpringContextHolder;
import com.xxl.job.executor.service.IEnShiftService;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;

@Service("EnShiftService")
public class EnShiftService implements IEnShiftService {
    private Connection connPre = null;

    @Override
    public void syncRecord() throws Exception{
        final String recordName = "tbl_qxsjzInShiftMiddle";
        try {
            DataSource dataSourcePre = SpringContextHolder.getBean("dataSourcePre");
            connPre = DataSourceUtils.getConnection(dataSourcePre);
            connPre.setAutoCommit(false);

            String recordStr = DBUtils.getCurSynRecordNum(connPre, recordName);
            XxlJobLogger.log("[普通]本次最大记录RECORDID为:" + recordStr);

            String dbCurTime = DBUtils.getDBCurTimeBefore3Min(connPre);
            XxlJobLogger.log("[普通]本次同步截止时间为:" + dbCurTime);

            XxlJobLogger.log("=====开始执行业务操作===");
            StringBuffer sql = new StringBuffer();
            sql.append("insert into TB_SYNC_ENSHIFT(SEQ_ID,STATION_ID,SQUAD_DATE,SQUAD_NO,LANE_NO,TOTAL_COUNT,SQUARD_TYPE,OPERATORNO,INSERT_TIME)" +
                    "select SEQ_SYNC_ENSHIFT_ID.nextval,StationNo,SquadDate,SquadNo,LaneNo,totalcount,'mtc',OPERATORNO,sysdate"+
                    " from TBL_qxsjzInShiftMiddle"+
                    " where totalcount > 0"+
                    " and inserttabletime>=to_date('" + recordStr + "','yyyy-mm-dd hh24:mi:ss') and inserttabletime<to_date('" + dbCurTime + "','yyyy-mm-dd hh24:mi:ss') ");
            Integer count = DBUtils.execute(connPre, sql.toString());
            XxlJobLogger.log("[普通流水]本次MTC入口工班共同步(条):" + count);

            String sqlPro = "insert into TB_SYNC_ENSHIFT(SEQ_ID,STATION_ID,SQUAD_DATE,SQUAD_NO,LANE_NO,TOTAL_COUNT,SQUARD_TYPE,OPERATORNO,INSERT_TIME)" +
                    " select SEQ_SYNC_ENSHIFT_ID.nextval,StationNo,SquadDate,SquadNo,LaneNo,totalcount,'etc',OPERATORNO,sysdate"+
                    " from TBL_qxsjzShiftMiddleETC"+
                    " where laneNo < 50 and inserttabletime>=to_date('" + recordStr + "','yyyy-mm-dd hh24:mi:ss') and inserttabletime<to_date('" + dbCurTime + "','yyyy-mm-dd hh24:mi:ss') ";
            count = DBUtils.execute(connPre, sqlPro);
            XxlJobLogger.log("[普通流水]本次ETC入口工班共同步(条):" + count);

            count = DBUtils.modifySynRecordNum(connPre, recordName, recordStr, dbCurTime);
            if (count == null || count != 1) {
                XxlJobLogger.log("[异常][普通流水]更新记录时间异常");
                throw new Exception("[普通流水]更新记录时间异常");
            }
            XxlJobLogger.log("[普通流水]更新记录时间为:" + dbCurTime);

            connPre.commit();
        }catch(Exception e){
            if (connPre != null) {
                try {
                    connPre.rollback();
                    XxlJobLogger.log("回滚成功.");
                }catch (Exception e1){
                    e1.printStackTrace();
                }
            }
            throw e;
        }finally{
            if (connPre != null) {
                try {
                    connPre.setAutoCommit(true);
                    connPre.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}
